import 'package:encrypt_and_decrypt_files/encrypt_page/encrypt_page.dart';
import 'package:encrypt_and_decrypt_files/history_list/history_list_page.dart';
import 'package:encrypt_and_decrypt_files/my/my_page.dart';
import 'package:encrypt_and_decrypt_files/widget/extension.dart';
import 'package:encrypt_and_decrypt_files/widget/multi_touch_gesture_detector.dart';
import 'package:flutter/material.dart';

class HomePage extends StatefulWidget {
  const HomePage({
    super.key,
  });

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  late List<Widget> _pageList;

  /// Navigation bar name
  final List<String> _appBarTitles = ['index', 'history', 'my'];

  /// Navigation bar icon
  List<BottomNavigationBarItem> items = [];

  /// Paging controller
  final PageController _pageController = PageController();

  @override
  void initState() {
    super.initState();

    _pageList = [
      _createPage(const EncryptPage()),
      _createPage(const HistoryListPage()),
      _createPage(const MyPage()),
    ];
  }

  // 创建每个页面的通用方法
  Widget _createPage(Widget page) {
    return MultiTouchGestureDetector(
      child: page,
      onPanUpdate: (int fingers, double verticalDelta) {
        if ((fingers == 3 || fingers == 2) && verticalDelta > 0) {
          /// 三指向下滑动 跳首页
          jumpTo(0);
        }
      },
      onScaleUpdate: (int fingers, double scale) {
        if (fingers == 4 || fingers == 5) {
          if (scale > 1) {
            /// 放大
            jumpTo(1);
          } else if (scale < 1) {
            /// 缩小
            jumpTo(2);
          }
        }
      },
    );
  }

  void jumpTo(int index) {
    _pageController.jumpToPage(index);
    setState(() {
      currentIndex = index;
    });
  }

  /// ADAPTS photo width to the screen
  static const double _imageSize = 25.0;

  /// ICONS for selected and unselected states
  static List<List<dynamic>> tabImages = [
    [
      Image.asset('assets/images/home/icon_home_unselect.png',
          width: _imageSize),
      Image.asset('assets/images/home/icon_home_select.png', width: _imageSize)
    ],
    [
      Image.asset('assets/images/home/icon_order_unselect.png',
          width: _imageSize),
      Image.asset('assets/images/home/icon_order_select.png',
          width: _imageSize),
    ],
    [
      Image.asset('assets/images/home/icon_me_unselect.png', width: _imageSize),
      Image.asset('assets/images/home/icon_me_select.png', width: _imageSize),
    ]
  ];

  List<BottomNavigationBarItem> _buildBottomNavigationBarItem(
      BuildContext context) {
    items = List.generate(3, (i) {
      return BottomNavigationBarItem(
        icon: tabImages[i][0],
        activeIcon: tabImages[i][1],
        label: _appBarTitles[i].t(context),
      );
    });
    return items;
  }

  int currentIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        backgroundColor: Colors.white,
        type: BottomNavigationBarType.fixed,
        items: _buildBottomNavigationBarItem(context),
        elevation: 5.0,
        iconSize: 20.0,
        selectedFontSize: 10,
        unselectedFontSize: 10,
        currentIndex: currentIndex,
        unselectedItemColor: Colors.black,
        selectedItemColor: const Color(0xFFFF8547),
        onTap: (index) {
          jumpTo(index);
        },
      ),
      body: PageView(
        physics: const NeverScrollableScrollPhysics(),
        controller: _pageController,
        children: _pageList,
      ),
    );
  }
}
